# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.

# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("canvas")

# 设置jniLibs目录路径
set(JNILIBS_DIR ${PROJECT_SOURCE_DIR}/../jniLibs)

# 添加静态库搜索路径
link_directories(${JNILIBS_DIR}/${ANDROID_ABI})


set(QUICKJS_PATH "${PROJECT_SOURCE_DIR}/quickjs")

# quickjs define
file(STRINGS "${QUICKJS_PATH}/VERSION" CONFIG_VERSION)
add_definitions(-DCONFIG_VERSION=\"${CONFIG_VERSION}\")
add_definitions(-DDUMP_LEAKS=1)
add_definitions(-DCONFIG_BIGNUM)

file(GLOB quickjs_src
        "${QUICKJS_PATH}/cutils.c"
        "${QUICKJS_PATH}/cutils.h"
        "${QUICKJS_PATH}/libbf.c"
        "${QUICKJS_PATH}/libbf.h"
        "${QUICKJS_PATH}/libregexp-opcode.h"
        "${QUICKJS_PATH}/libregexp.c"
        "${QUICKJS_PATH}/libregexp.h"
        "${QUICKJS_PATH}/libunicode-table.h"
        "${QUICKJS_PATH}/libunicode.c"
        "${QUICKJS_PATH}/libunicode.h"
        "${QUICKJS_PATH}/list.h"
        "${QUICKJS_PATH}/quickjs-atom.h"
        "${QUICKJS_PATH}/quickjs-opcode.h"
        "${QUICKJS_PATH}/quickjs.c"
        "${QUICKJS_PATH}/quickjs.h"
        "${QUICKJS_PATH}/quickjs-libc.h"
        "${QUICKJS_PATH}/quickjs-libc.c"
)

set(CANVAS_PATH "${PROJECT_SOURCE_DIR}/canvas")
file(GLOB canvas_src
        "${CANVAS_PATH}/Canvas.cc"
        "${CANVAS_PATH}/Canvas.h"
        "${CANVAS_PATH}/Context.cc"
        "${CANVAS_PATH}/Context.h"
        "${CANVAS_PATH}/CanvasJSBinding.cc"
        "${CANVAS_PATH}/CanvasJSBinding.h"
        "${CANVAS_PATH}/ContextJSBinding.cc"
        "${CANVAS_PATH}/ContextJSBinding.h"
)

# 查找目录下的所有头文件
include_directories(
        ${CMAKE_SOURCE_DIR}/skia
        ${CMAKE_SOURCE_DIR}/skia/include
        ${CMAKE_SOURCE_DIR}/skia/modules
        ${CMAKE_SOURCE_DIR}/eventbus
        ${CMAKE_SOURCE_DIR}/event
        ${CMAKE_SOURCE_DIR}/canvas
        ${CMAKE_SOURCE_DIR}/engine
        src/main/cpp
)

add_library(${CMAKE_PROJECT_NAME} SHARED
        # List C/C++ source files with relative paths to this CMakeLists.txt.
        Entrance.cpp CustomSkiaDraw.cc FontManager.cc JSAppBinding.cc
        engine/JSApp.cc
        engine/JSEngine.cc
        engine/qjs.c
        canvas/CanvasJSBinding.cc
        canvas/ContextJSBinding.cc
        canvas/Canvas.cc
        canvas/Context.cc
        ${quickjs_src})


# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
find_library(log-lib log)
find_library(z-lib z)
find_library(android-lib android)

target_link_libraries(${CMAKE_PROJECT_NAME}
        android EGL GLESv3
        ${log-lib} ${z-lib} ${android-lib})

# 使用GLOB_RECURSE命令搜索所有的静态库文件
file(GLOB_RECURSE LIB_FILES ${JNILIBS_DIR}/*.a)
# 遍历每个静态库文件
foreach (LIB_FILE ${LIB_FILES})
    # 获取静态库文件的路径
    get_filename_component(LIB_PATH ${LIB_FILE} DIRECTORY)

    # 获取静态库文件的名称
    get_filename_component(LIB_NAME ${LIB_FILE} NAME_WE)

    # 添加静态库搜索路径
    link_directories(${LIB_PATH})

    # 引入静态库
    add_library(${LIB_NAME} STATIC IMPORTED)

    # 设置静态库路径
    set_target_properties(${LIB_NAME} PROPERTIES IMPORTED_LOCATION ${LIB_FILE})

    # 链接静态库到目标库
    target_link_libraries(${CMAKE_PROJECT_NAME} ${LIB_NAME})
endforeach ()